/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ElectreDialog.java
 *
 * Created on Jun 1, 2010, 9:39:56 PM
 */

package org.electre.ui;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.electre.core.Atributo;
import org.electre.utils.MyTableModel;

/**
 *
 * @author william
 */
public class ElectreDialog extends javax.swing.JDialog {
    private MyTableModel tableModel;
    private List<Atributo> atributos;

    /** Creates new form ElectreDialog */
    public ElectreDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.jPanel6.setLayout(new GridBagLayout());
        addComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSpinner1 = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        jSpinner2 = new javax.swing.JSpinner();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel6 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        finishButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(org.openide.util.NbBundle.getMessage(ElectreDialog.class, "ElectreDialog.title")); // NOI18N

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(ElectreDialog.class, "ElectreDialog.jPanel2.border.title"))); // NOI18N

        jLabel1.setText(org.openide.util.NbBundle.getMessage(ElectreDialog.class, "ElectreDialog.jLabel1.text")); // NOI18N

        jSpinner1.setModel(new javax.swing.SpinnerNumberModel(1, 1, 10, 1));

        jLabel2.setText(org.openide.util.NbBundle.getMessage(ElectreDialog.class, "ElectreDialog.jLabel2.text")); // NOI18N

        jSpinner2.setModel(new javax.swing.SpinnerNumberModel(1, 1, 10, 1));
        jSpinner2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner2StateChanged(evt);
            }
        });

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(ElectreDialog.class, "ElectreDialog.jPanel5.border.title"))); // NOI18N

        jPanel6.setBackground(new java.awt.Color(255, 255, 255));
        jPanel6.setForeground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 366, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 196, Short.MAX_VALUE)
        );

        jScrollPane2.setViewportView(jPanel6);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel3.setText(org.openide.util.NbBundle.getMessage(ElectreDialog.class, "ElectreDialog.jLabel3.text")); // NOI18N

        jLabel4.setText(org.openide.util.NbBundle.getMessage(ElectreDialog.class, "ElectreDialog.jLabel4.text")); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(72, 72, 72)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(18, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(276, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addGap(47, 47, 47)
                .addComponent(jLabel4)
                .addGap(77, 77, 77))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(49, 49, 49))
        );

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, 440, 390));

        finishButton.setText(org.openide.util.NbBundle.getMessage(ElectreDialog.class, "ElectreDialog.finishButton.text")); // NOI18N
        finishButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                finishButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(379, Short.MAX_VALUE)
                .addComponent(finishButton, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 456, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(finishButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void finishButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_finishButtonActionPerformed
        // TODO add your handling code here:
        /*
        BigDecimal total = this.updateTotal();
        total = total.setScale(4,BigDecimal.ROUND_DOWN);
        if (total.doubleValue() == 1d) {
         *
         */
            atributos = new ArrayList<Atributo>();
            tableModel = new MyTableModel();
            //int i = 1;
            tableModel.addColumn("Alternativas");
            String name = "";
            boolean positivo = false;
            for (Component c : jPanel6.getComponents()) {
                    if (c instanceof JTextField) {
                        tableModel.addColumn(((JTextField)c).getText());
                        name = ((JTextField)c).getText();
                    }else{
                        if (c instanceof JComboBox) {
                            positivo = ((JComboBox)c).getSelectedItem().equals("mayor es mejor");
                        }else{
                            double weight = (Double)((JSpinner)c).getValue();
                            atributos.add(new Atributo(name,positivo,weight));
                        }

                    }
            }

            for (int i = 0; i < (Integer)jSpinner1.getValue(); i++) {
                Vector v = new Vector();
                int j = 0;
                for (Component c : jPanel6.getComponents()) {
                     if (j==0) {
                         v.add(i+1);
                     }else{
                         v.add("");
                     }
                     j++;
                }
                tableModel.addRow(v);

            }
            //this.tableModel = model;
            //this.atributos = atributos;
            this.dispose();
        /*}else{
            JOptionPane.showMessageDialog(this, "La suma de las ponderaciones debe ser de 1", "Error",0);
        }
         *
         */
    }//GEN-LAST:event_finishButtonActionPerformed

    private void jSpinner2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner2StateChanged
        // TODO add your handling code here:
        Integer value = (Integer) jSpinner2.getValue();
        if (value > this.jPanel6.getComponentCount()/3) {
            this.addComponents();
        }
        if (value < this.jPanel6.getComponentCount()/3) {
            this.jPanel6.remove(this.jPanel6.getComponentCount() - 1);
            this.jPanel6.remove(this.jPanel6.getComponentCount() - 1);
            this.jPanel6.remove(this.jPanel6.getComponentCount() - 1);
        }
        this.jPanel6.revalidate();
        this.jPanel6.repaint();
        this.updateTotal();
    }//GEN-LAST:event_jSpinner2StateChanged

     private void addComponents() {
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridy = this.jPanel6.getComponentCount() / 2;
        c.gridx = 0;
        c.insets = new Insets(2,2,2,2);
        JTextField textField = new JTextField(20);
        c.fill = GridBagConstraints.HORIZONTAL;
        this.jPanel6.add(textField, c);
        c.gridx = 1;
       // DefaultComboBoxModel model = new DefaultComboBoxModel();
        JComboBox combo = new JComboBox(new String[]{"mayor es mejor","menor es mejor"});
        this.jPanel6.add(combo, c);
        c.gridx = 2;
        JSpinner spinner = new JSpinner();
        spinner.setModel(new javax.swing.SpinnerNumberModel(Double.valueOf(0.1), Double.valueOf(0.01), Double.valueOf(1), Double.valueOf(0.01)));
        spinner.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                ElectreDialog.this.updateTotal();
            }

        });
        this.jPanel6.add(spinner, c);
    }
    private BigDecimal updateTotal() {
        BigDecimal aux = BigDecimal.ZERO;
         for (Component c : jPanel6.getComponents()) {
                if (c instanceof JSpinner) {
                    aux = aux.add(new BigDecimal((Double)((JSpinner)c).getValue()).setScale(4,BigDecimal.ROUND_DOWN));
                }
        }
        DecimalFormat cf = new DecimalFormat("#.####");
        this.jLabel4.setText(cf.format(aux));
        return aux;
    }

    public MyTableModel getTableModel() {
        return tableModel;
    }

    public List<Atributo> getAtributos() {
        return atributos;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ElectreDialog dialog = new ElectreDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton finishButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JSpinner jSpinner2;
    // End of variables declaration//GEN-END:variables



}
